package com.szpx.service.impl;

import com.szpx.entity.Goods;
import com.szpx.entity.Result;
import com.szpx.entity.TBUser;
import com.szpx.mapper.TBUserMapper;
import com.szpx.service.TBUserService;
import com.szpx.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author June
 * @version 1.0.0
 * @title TBUserServiceImpl
 * @create 2024/4/2 14:30
 * @description
 **/
@Service
@Transactional
public class TBUserServiceImpl implements TBUserService {

    @Autowired
    private TBUserMapper tbUserMapper;

    /**
     * 注册
     *
     * @param tbUser
     * @return
     */
    @Override
    public boolean register(TBUser tbUser) {
        // 设置注册时间
        tbUser.setRegtime(LocalDateTime.now());

        String password = tbUser.getPassword();
        String encrypt = MD5.encrypt(password);

        tbUser.setPassword(encrypt);
        return tbUserMapper.addUser(tbUser) == 1;
    }

    /**
     * 登录
     *
     * @param tbUser
     * @return
     */
    @Override
    public TBUser login(TBUser tbUser) {
        // 根据手机号查询出的数据库用户
        TBUser u = tbUserMapper.selectByPhone(tbUser);
        // 不存在手机号的情况
        if (u == null) {
            return null;
        }
        // 用户传来的要校验的明文登录密码和数据库密文进行校验
        boolean flag = MD5.validate(tbUser.getPassword(), u.getPassword());
        return flag ? u : null;
    }

    /**
     * 根据ID查询信息
     *
     * @param uid
     * @return
     */
    @Override
    public TBUser selectByUid(Integer uid) {
        return tbUserMapper.selectByUid(uid);
    }

    /**
     * 查询全部用户信息
     *
     * @return
     */
    @Override
    public List<TBUser> findAll() {
        return tbUserMapper.findAll();
    }

    /**
     * 用户账户管理修改用户email
     * @param tbUser
     */

    @Override
    public void updateUserEmail(TBUser tbUser) {
        tbUserMapper.updateUserEmail(tbUser);

    }

    /**
     * 修改用户名
     * @param tbUser
     */
    @Override
    public void updateUserName(TBUser tbUser) {
        tbUserMapper.updateUserName(tbUser);

    }

    /**
     * 修改手机号
     * @param tbUser
     */
    @Override
    public void updateUserPhone(TBUser tbUser) {
        tbUserMapper.updateUserPhone(tbUser);

    }

    /**
     * 用户充值
     *
     * @param uid
     * @param amount
     * @return
     */
    @Override
    public boolean updateUserBalance(Integer uid, double amount) {
        if (uid == null || amount <= 0) {
            throw new IllegalArgumentException("用户ID和充值金额必须大于0");
        }

        // 获取原来的余额
        TBUser tbUser = tbUserMapper.selectByUid(uid);

        if (tbUser == null) {
            throw new RuntimeException("用户不存在，无法进行充值");
        }

        // 计算充值后的余额
        double originalBalance = tbUser.getBalance();
        double newBalance = originalBalance + amount;

        // 将更新后的余额保存到数据库
        int rowsAffected = tbUserMapper.updateUserBalance(uid, newBalance);

        if (rowsAffected <= 0) {
            throw new RuntimeException("更新用户余额失败");
        }

        return true;
    }
}

